<template>
  <div class="privilege">
    <!-- 按钮 -->
    <div class="btns">
      <el-button type="primary" size="small" @click="toAdd">添加</el-button> 
      <el-button type="primary" size="small" @click="loadPrivilegeData">查询所有权限</el-button>
      <el-button type="primary" size="small" @click="loadPrivilegeData1">查询父编号权限</el-button>
    </div>
    <!-- 按钮 -->
          
    <!-- 内容 -->
    <el-table size="small" :data="privilege">
      <el-table-column label="序号" prop="id"></el-table-column>
      <el-table-column label="权限名" prop="name"></el-table-column>
      <el-table-column label="路由" prop="route"></el-table-column>
      <el-table-column label="类型" prop="type"></el-table-column>
      <el-table-column label="图标" prop="icon"></el-table-column>
      <el-table-column label="隐藏" prop="hidden"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template v-slot:default="scope">
          <el-button type="text" size="mini" @click="editPrivilege(scope.row)" >修改</el-button>
          <el-button type="text" size="mini" @click="delPrivilege(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /内容 -->
    <!-- 分页  v-if="privilege.total > param.pageSize"-->
   <!-- <el-pagination
      layout="prev, pager, next"
      @current-change="currentChangeHandler"
      :current-page="param.page"
      :page-size="param.pageSize"
      :total="privilege.total">
    </el-pagination>-->
    <!-- /分页 -->
    <!-- 模态框 -->
    <el-dialog title="权限" :visible.sync="visible" width="60%">
      <el-form label-width="80px" size="small">
        <el-form-item label="序号">
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="权限名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="路由">
          <el-input v-model="form.route"></el-input>
        </el-form-item>       
        <el-form-item label="类型">
          <el-input v-model="form.type"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="隐藏">
          <el-radio-group v-model="form.hidden">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="父编号">
          <el-input v-model="form.parent_id"></el-input>
        </el-form-item>
      </el-form>      
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>

<script>
// 1. 导入请求函数
import {
  get,post
}from '@/utils/request'

export default {
  data(){
    return {
      visible:false,
      /*param:{
        page:1,
        pageSize:10
      },*/
      privilege:{

      },
    form:{
       hidden:"1"
      }
    }
  },
    // 2.在vue实例创建完毕之后就调用加载用户信息的接口
  created(){
    this.loadPrivilegeData();
    this.loadPrivilegeData1();
  },

  methods:{
    
    loadPrivilegeData(){
      let url ="/privilege/findAll";
      get(url).then(response => {
        this.privilege = response.data;
      })
    },
    loadPrivilegeData1(){
      let url ="/privilege/findByParentId";
      get(url).then(response => {
        this.privilege = response.data;
      })
    },
    // 修改用户信息
    editPrivilege(row){
      // 将要修改的信息赋值给当前表单，进行双向数据绑定，再次基础上完成修改操作
      this.form = row;
      this.visible = true;
    },

    // 删除用户信息
    delPrivilege(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/privilege/deleteById";
        get(url,{id:row.id}).then(response => {
          this.$message({type:"success",message:response.message});
          this.loadPrivilegeData();
        })
      })
    },

    // 提交表单
    submit(){
      // 1. 获取表单数据  this.form
      // 2. 交互
      let url = "/privilege/saveOrUpdate"
      post(url,this.form).then(response => {
        // 3. 提示
        this.$message({type:"success",message:response.message});
        // 4. 关闭模态框
        this.visible = false;
        // 5. 刷新数据
        this.loadPrivilegeData();
      })
     
    },
    // 打开模态框去添加，
    toAdd(){
      this.form = {hidden:"1"}
      this.visible = true;
    },
     // 翻页
   /* currentChangeHandler(page){
      this.param.page = page;
      this.loadPrivilegeData();
    }*/
  }
}
</script>